package net.neptech.listener;

import net.neptech.model.emum.OrderStatus;
import net.neptech.service.OrderService;
import net.neptech.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

@Service
public class OrderListeners {

    @Autowired
    OrderService orderService;


    /**
     * 删除订单
     */
    @KafkaListener (topics = "order_topic", groupId = "cloud-med-group-order")
    public void orderHandle(@Payload Long id, @Header(KafkaHeaders.RECEIVED_KEY) String key, Acknowledgment ack) {
        try {
            boolean deleteOrder = key.equals("deleteOrder");
            if (deleteOrder){
                orderService.deleteOrder(id);
                ack.acknowledge();
            }else{
                //更新订单支付状态
               orderService.updateOrder(id, OrderStatus.PAID.getCode());
            }
        }catch (Exception e){
            throw  e;
        }
    }
}
